package vocation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jfinal.aop.Duang;
import com.jfinal.kit.Ret;
import common.model.Department;
import common.model.User;
import common.model.Vocation;
import layuiTable.layuiTableVocation;

import java.util.Date;
import java.util.List;

public class VocationService {

    public static final VocationService me = Duang.duang(VocationService.class);
    private static final Vocation vocationDao = new Vocation().dao();
    private static final Department departmentDao = new Department().dao();

    public String getVocationString(User user, String status) {
        List<Vocation> vocations = getVocation(user, status);
        return JSON.toJSONString(new layuiTableVocation(0, "", vocations.size(), vocations), SerializerFeature.WriteDateUseDateFormat);
    }

    public List<Vocation> getVocation(User user, String status) {
        List<Vocation> vocations;
        if (status.equals("我申请的")) {
            vocations = vocationDao.find(vocationDao.getSqlPara("vocation.findAllByUser", user.getId()));
        } else {
            String key = status.equals("审核中") ? "Wait" : "Checked";
            if (user.getAdmin()) {
                vocations = vocationDao.find(vocationDao.getSql("vocation.find" + key));
            } else if (user.getLevel().equals("部门经理")) {
                vocations = vocationDao.find(vocationDao.getSqlPara("vocation.find" + key + "ByDepartment", user.getDepartment()));
            } else {
                vocations = vocationDao.find(vocationDao.getSqlPara("vocation.find" + key + "ByUser", user.getId()));
            }
        }
        return vocations;
    }

    public Ret newVocation(User user, String reason, Date startDay, Date endDay) {
        Vocation vocation = new Vocation();
        Department department = departmentDao.findById(user.getDepartment());
        if (!vocation.setEmp(user.getId()).setEmpName(user.getUsername()).setStartDay(startDay).setEndDay(endDay).setReason(reason).setDepartment(user.getDepartment()).setDName(department.getDName()).save()) {

            return Ret.by("status", false).set("msg", "vocation表异常");
        } else {
            return Ret.by("status", true);
        }
    }

    public Ret agreeVocation(int id) {
        Vocation vocation = vocationDao.findById(id);
        if (!vocation.setStatus("已批准").update()) {
            return Ret.by("status", false).set("msg", "vocation表异常");
        } else {
            return Ret.by("status", true);
        }
    }

    public Ret refuseVocation(int id) {
        Vocation vocation = vocationDao.findById(id);
        if (!vocation.setStatus("已拒绝").update()) {
            return Ret.by("status", false).set("msg", "vocation表异常");
        } else {
            return Ret.by("status", true);
        }

    }

    public Ret deleteVocation(int id, User user) {
        Vocation vocation = vocationDao.findById(id);
        if (vocation.getEmp() != user.getId()) {
            return Ret.by("status", false).set("msg", "您非此休假发起者");
        }
        if (!vocation.setStatus("用户撤销").update()) {
            return Ret.by("status", false).set("msg", "vocation表异常");
        } else {
            return Ret.by("status", true);
        }
    }

}
